/**
 * Created by Administrator on 2015/11/30.
 */
var debug = console.log;
var a = {
    name :'dongge',
    age :20
}

var b ={
    hello:function(){
        debug('Hello '+this.name);
    }
};



a.__proto__ = b;
//a.prototype = b;
console.log(a);//{ name: 'dongge', age: 20, __pro__: { hello: [Function] } }
console.log(a.__proto__);//{ hello: [Function] }
debug(Object.getPrototypeOf(a));//{ hello: [Function] }
debug(a.prototype);//undefined
console.log(Object.getPrototypeOf(Object.getPrototypeOf(a)));//{}

debug('+++++++++++++++++++++++++++');
function Person(name,age){
    this.name = name;
    this.age  = age;
}

function display(){}
debug(Object.getPrototypeOf(display));
debug(Object.getPrototypeOf(Person));

Person.prototype.hello = function(){
    debug('Hello '+this.name);
};
debug(Object.getPrototypeOf(Person));

debug('--------------------------------');
var p = new Person('dongge',12);
debug(p);//Person { name: 'dongge', age: 12 }
debug(Object.getPrototypeOf(p));//Person { hello: [Function] }
debug(Object.getPrototypeOf(Object.getPrototypeOf(p)));//{}
debug(p instanceof Person);//true
debug(p instanceof Object);//true
debug(p instanceof Function);//false
debug(Person instanceof Function);//true
debug(Function instanceof Object);//true
debug('*********华丽的分割线*********')

var p1 = Object.create(Person.prototype);
Person.call(p1,'zeus',10);
debug(p1);//Person { name: 'zeus', age: 10 }
debug(p1 instanceof Person);//true
debug(p1.__proto__);//Person { hello: [Function] }

debug('!!!!!!!!!!!!!!非常强的分割线!!!!!!!!!!!!!!!!');
var p2 = {};
Person.call(p2,'xx',1);
p2.__proto__ = Person.prototype;
debug(p2 instanceof Person);//true
debug(Person.prototype.__proto__)
debug(Object.getPrototypeOf(Person.prototype))
debug(Object.getPrototypeOf(Person));
debug(Person.__proto__)

debug('%%%%%%%%%%%%%%%%%%%%%%%%%%')
function myinstanceof(obj,superobj){
    var objproto = Object.getPrototypeOf(obj);

    while(objproto!==null){
        if(objproto === superobj.prototype){
            return true;
        }
        objproto = Object.getPrototypeOf(objproto);
    }
}
debug(myinstanceof(p2,Object));
debug('++++++++++++++++++++++++++');
+function(){
    var obj = {};
    obj.age = 1;
    obj.prototype = {};
    obj.prototype.hello = function(){
        console.log('Hello');
    }
    var subObj = Object.create(obj.prototype);
    subObj.hello();
    var subObj1 = Object.create(obj);
    console.log(subObj1);
}();
+function(){
    function Person(){
        this.name = 'zeus';
        this.age = 19;
    }
    Person.sayHello = function (){
        console.log('sayHello');
    }
    Person.prototype.hello = function(){
        console.log(this.name+this.age);
    }
    var p = new Person();
    p.hello();
    //p.sayHello();error
}();
+function(){
    function f(){xx}
    var proto = f.prototype;
    console.log(proto);
    console.log(typeof proto)
    console.log(proto instanceof Object)
    console.log(proto instanceof Function)
    /**
     * 函数的prototype是一个包含constructor属性和__proto___原型链的对象
     */
}();
+function(){
    console.log('-------------------------------')
    function Person(){
        this.name = 'zeus';
        this.age = 19;
    }
    Person.prototype.hello = function(){
        console.log(this.name+this.age);
    };
    function Manager(){
        this.type = 'manager';
    }
    Manager.prototype = new Person();
    Manager.prototype.hello = function(){
        console.log('ddddd');
    }
    Manager.prototype.constructor = Manager;
    var m  = new Manager();
    console.log(m instanceof Manager);
    console.log(m instanceof Person);
    Person.prototype.sayYes = function(){
        console.log('sayYes');
    }
    m.hello();
    m.sayYes();
    /**
     * 这里实现了原型继承
     * Manager.prototype = new Person()会覆盖掉原本prototype中的constructor属性
     * 这使得m.constructor无法获取正确的构造函数，所以这里后面又设置了constructor
     * 但是这并不影响对象的构建，所以该字段意义其实并不大。
     * 使用typeof并不能区别person和manager，这里只能使用instanceof。
     */
}();